﻿package com.projects.xenia.add_activities;

import java.util.ArrayList;
import java.util.Set;

import com.projects.xenia.R;
import com.projects.xenia.documents_classes.Documents_Order;
import com.projects.xenia.documents_classes.Documents_Order.DocLine;
import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.ref_classes.OffersRefItem;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;



public class Activity_Offers extends Activity {
		
	private ListView OffersListView;
	private OffersCustomListAdapter OffersListViewAdapter;
	private ArrayList<OffersRefItem> OffersList;
	
	private ImageView offers_form_BackWard;
	
	private Context Ctx;

	private Documents_Order DocumentClass;
	
	private ArrayList<String> OffersAssortment;
	
	private void GetOffersList () {
		
		OffersList = new ArrayList<OffersRefItem> ();
				
		Cursor OffersCursor = MainGlobalModule.DBE.rawQuery("select id, descr, descr_full, date_begin, date_end, offers_condition_type, offers_condition_logical_type, offers_condition_total_value from xref_offers order by descr", null);
		
		if (OffersCursor.getCount()>0) {
			OffersCursor.moveToFirst();
			while (!OffersCursor.isAfterLast()) {
				
				OffersRefItem _OffersRefItem = new OffersRefItem ();		
				_OffersRefItem.id = OffersCursor.getString(0);
				_OffersRefItem.descr = OffersCursor.getString(1);
				_OffersRefItem.descr_full  = OffersCursor.getString(2);
				_OffersRefItem.date_begin  = MainGlobalModule.SqlStringToDate(OffersCursor.getString(3)); 
				_OffersRefItem.date_end  = MainGlobalModule.SqlStringToDate(OffersCursor.getString(4));
				_OffersRefItem.offers_condition_type  = OffersCursor.getInt(5);
				_OffersRefItem.offers_condition_logical_type  = OffersCursor.getInt(6); 
				_OffersRefItem.offers_condition_total_value  = OffersCursor.getDouble(7);
				_OffersRefItem.offers_condition_exists = 0;
				
				
				OffersAssortment = MainGlobalModule.GetOffersAssortmentsList (_OffersRefItem.id);
				
				Set<String> keys = DocumentClass.DocLineCollection.keySet();
				for (String key : keys){
					DocLine dl = DocumentClass.DocLineCollection.get(key);
					if (OffersAssortment.contains(dl.goods_id)) {
						_OffersRefItem.offers_condition_exists = 1;
						break;
					}
				}				
				
				if (MainGlobalModule.ExistsBonusByOffers(DocumentClass.Head.id, _OffersRefItem.id)) {
					_OffersRefItem.offers_condition_exists = 2;
				}
				
				OffersList.add(_OffersRefItem);
				
				OffersCursor.moveToNext();
				
			}
		}
		
		OffersCursor.close();
		
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);		
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);            
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
	
		setContentView(R.layout.offers_form);
		
		offers_form_BackWard = (ImageView)findViewById(R.id.offers_form_BackWard);
		offers_form_BackWard.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				finish ();
			}
		});
		
		DocumentClass = (Documents_Order)MainGlobalModule.DocumentClass;
		
		Ctx = this;
		
		Bundle DocExtras;
		DocExtras = getIntent().getExtras();
		
		GetOffersList ();
		
		OffersListView = (ListView)findViewById(R.id.offers_form_ListView);
		OffersListViewAdapter = new OffersCustomListAdapter (this);
		OffersListView.setAdapter(OffersListViewAdapter);
		
		OffersListView.setOnItemClickListener(OffersListView_ItemClick);
		
	}
	
	private OnItemClickListener OffersListView_ItemClick = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			
			OffersRefItem it = OffersListViewAdapter.getItem(arg2);	
			Intent OffersConditionsIntent = new Intent ();			
			OffersConditionsIntent.setClass(Ctx,Activity_OffersConditions.class);
			OffersConditionsIntent.putExtra("id", it.id);			
			OffersConditionsIntent.putExtra("descr", it.descr);
			OffersConditionsIntent.putExtra("descr_full", it.descr_full);			
			OffersConditionsIntent.putExtra("offers_condition_type", it.offers_condition_type);
			OffersConditionsIntent.putExtra("offers_condition_logical_type", it.offers_condition_logical_type);			
			OffersConditionsIntent.putExtra("offers_condition_total_value", it.offers_condition_total_value);
			
			startActivityForResult(OffersConditionsIntent, 0);
			
		}
		
	};

	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		GetOffersList ();
		OffersListViewAdapter.notifyDataSetChanged();		
		
	};
	
	private class OffersCustomListAdapter extends BaseAdapter  {

		private LayoutInflater mInflater;
		public Context context;		
		  
		public OffersCustomListAdapter(Context context) { 
			
			mInflater = LayoutInflater.from(context);
			this.context = context;
			
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
		    
			ViewHolder holder;
			
			if (convertView == null) {			
				convertView = mInflater.inflate(R.layout.offers_form_list, null);
			}
			
			holder = new ViewHolder();
			
			OffersRefItem it  = getItem(position);
			
			holder.num = (TextView)convertView.findViewById(R.id.offers_formlist_num);
			holder.descr = (TextView)convertView.findViewById(R.id.offers_formlist_descr);
			holder.descr_full = (TextView)convertView.findViewById(R.id.offers_formlist_descr_full);
			holder.descr_add = (TextView)convertView.findViewById(R.id.offers_formlist_adddescr);
			
			holder.num.setText(String.valueOf (position+1));
			holder.descr.setText(it.descr);
			holder.descr_full.setText(it.descr_full);
			holder.descr_add.setText(String.format ("Тип условий:%s, Лог.объед:%s %nПериод с [%s] по [%s]" ,it.offers_condition_type==1 ? "сумма":"кол-во",it.offers_condition_logical_type==1 ? "И":"ИЛИ",MainGlobalModule.FormatDate(it.date_begin),MainGlobalModule.FormatDate(it.date_end)));			
			
			
			switch ((int)it.offers_condition_exists) {
			
				case 1:
					holder.num.setBackgroundColor(Color.YELLOW);
					break;
				case 2:
					holder.num.setBackgroundColor(Color.GREEN);
					break;
				default:
					holder.num.setBackgroundColor(Color.CYAN);
					break;
			}
			
			
			return convertView;
			
		}
		
		
		@Override
		public int getCount() {			
			return OffersList.size();
		}

		@Override
		public OffersRefItem getItem(int arg0) {		
			return OffersList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {			
			return OffersList.get(arg0).id.hashCode();
		}		
		
		class ViewHolder {
			
			TextView num;
			TextView descr;
			TextView descr_full;
			TextView descr_add;
				
		}
			 
		
	}
	
}
